var barqrGen = require('../../../utils/wxbarqrGen.js')
var app = getApp()
var reqCardId

Page({
  data: {},
  onLoad: function (options) {
    var that = this
    wx.getSystemInfo({
      success: function (res) {
        var hgt = (res.windowWidth - 60) * 390 / 669
        that.setData({
          cardhgt: hgt
        })
      },
    })
    this.setData({
      cardNO: app.globalData.cardNO
    })

    this.loadCard()
    this.reqBarCodeForTime()
  },
  reqBarCodeForTime: function () {
    var that = this
    reqCardId = setTimeout(() => {
      if (reqCardId) {
        clearTimeout(reqCardId)
        that.loadCard()
        that.reqBarCodeForTime()
      }
    }, 60000)
  },
  onHide: function () {
    if (reqCardId) {
      clearTimeout(reqCardId)
    }
  },
  loadCard: function () {
    app.showLoading()
    var url = '/yuanmai_we/user/paycode/gen'
    var property = {
      userId: app.globalData.userid
    }
    var params = {
      datas: JSON.stringify(property),
      __zaofans: true
    }
    app.request(url, params).then(res => {
      app.hideLoading()
      this.setData({
        barCode: res.payCode
      })
      barqrGen.barcode('barcode', res.payCode, 480, 360)
    })
  }
})
